package wps

import (
	"github.com/zzl/go-com/com"
	"github.com/zzl/go-win32api/v2/win32"
	"syscall"
	"unsafe"
)

var CLSID_KEXCELCtrlExtender = syscall.GUID{0x7BC8A1B5, 0xCF6E, 0x4AEB,
	[8]byte{0x9B, 0x50, 0x1B, 0xF5, 0xEC, 0xCF, 0xE2, 0xD9}}

type KEXCELCtrlExtender struct {
	IEXCELCtrlExtender
}

func NewKEXCELCtrlExtender(pUnk *win32.IUnknown, addRef bool, scoped bool) *KEXCELCtrlExtender {
	if pUnk == nil {
		return nil
	}
	p := (*KEXCELCtrlExtender)(unsafe.Pointer(pUnk))
	if addRef {
		pUnk.AddRef()
	}
	if scoped {
		com.AddToScope(p)
	}
	return p
}

func NewKEXCELCtrlExtenderInstance(scoped bool) (*KEXCELCtrlExtender, error) {
	var p *win32.IUnknown
	hr := win32.CoCreateInstance(&CLSID_KEXCELCtrlExtender, nil,
		win32.CLSCTX_INPROC_SERVER|win32.CLSCTX_LOCAL_SERVER,
		&IID_IEXCELCtrlExtender, unsafe.Pointer(&p))
	if win32.FAILED(hr) {
		return nil, com.NewError(hr)
	}
	return NewKEXCELCtrlExtender(p, false, scoped), nil
}
